<?php
/**
 * ***********************************************************************************
 * 咨询鲤 PC端知识店铺首页
 * 
 * @author Mr.RJH
 * @date 2025-09-02
 * @email<15213026209@163.com>
 * ***********************************************************************************
 */
namespace app\controller;

use app\BaseController;

use think\facade\Cache;
use think\facade\Request;

use app\model\merchant\ZxlKnowStorePcSettingModel;
use app\model\merchant\ZxlKnowStorePcSelfDomainModel;

use app\service\ApacheConfigService;

class DomainAudit extends BaseController
{
    /**
     * PC端商户申请独立域名审核通过域名
     * 
     * @return json
     */
    public function approve()
    {
        if ($this->request->isPost()) {

            $domainId = $this->request->param('domain_id');
            try {
                $domainRecord = ZxlKnowStorePcSelfDomainModel::find($domainId);
                if (!$domainRecord) {
                    throw new \Exception('域名申请记录不存在');
                }
                $set = ZxlKnowStorePcSettingModel::where('m_id', $domainRecord['m_id'])->find();
                if (!$set) {
                    throw new \Exception('PC端店铺设置不存在');
                }
                $apacheConfigService = new ApacheConfigService();
                if ($domainRecord['pem_url'] && $domainRecord['key_url']) {
                    $domainRecord['pem'] = @file_get_contents($domainRecord['pem_url']);
                    $domainRecord['key'] = @file_get_contents($domainRecord['key_url']);
                    
                    // 保存SSL证书
                    $certResult = $apacheConfigService->saveSSLCertificates(
                        $domainRecord->domain,
                        $domainRecord->pem ?? '',
                        $domainRecord->key ?? ''
                    );
                    if (!$certResult) {
                        throw new \Exception('SSL证书保存失败');
                    }
                }
                
                // 生成Apache配置
                $config = $apacheConfigService->generateVhostConfig(
                    $domainRecord,
                    $set['domain_url'],
                );

                // 部署配置
                $deployResult = $apacheConfigService->deployConfig(
                    $domainRecord->domain,
                    $config
                );
                if (!$deployResult) {
                    throw new \Exception('Apache配置部署失败');
                }
                
                // 更新数据库记录
                $domainRecord->save([
                    'status'        => 1,
                    'apache_config' => $config,
                    'audit_time'    => time(),
                ]);

                if ($deployResult['success']) {
                    // 获取部署摘要
                    $summary = $apacheConfigService->getDeploymentSummary($domainRecord->domain);
                    
                    return json(['code' => 0, 'msg' => 'SUCCESS', 'deploy' => $deployResult, 'summary' => $summary]);
                } else {
                    throw new \Exception($deployResult['error']);
                }
            } catch (\Exception $e) {
                return json(['code' => 1,'msg' => $e->getMessage()]);
            }
        }
    }

    /**
     * 更换安全证书
     * 
     * @return json
     */
    public function replace_ssl()
    {
        if ($this->request->isPost()) {

            $domainId = $this->request->param('domain_id');
            $pem_url = $this->request->param('pem_url');
            $key_url = $this->request->param('key_url');
            try {
                $domainRecord = ZxlKnowStorePcSelfDomainModel::find($domainId);
                if (!$domainRecord) {
                    throw new \Exception('域名申请记录不存在');
                }
                $set = ZxlKnowStorePcSettingModel::where('m_id', $domainRecord['m_id'])->find();
                if (!$set) {
                    throw new \Exception('PC端店铺设置不存在');
                }
                $apacheConfigService = new ApacheConfigService();
                $domainRecord['pem'] = @file_get_contents($pem_url);
                $domainRecord['key'] = @file_get_contents($key_url);
                
                // 保存SSL证书
                $certResult = $apacheConfigService->saveSSLCertificates(
                    $domainRecord->domain,
                    $domainRecord->pem ?? '',
                    $domainRecord->key ?? ''
                );
                if (!$certResult) {
                    throw new \Exception('SSL证书保存失败');
                }
                return json(['code' => 0, 'msg' => 'SUCCESS', 'data' => $certResult]);
            } catch (\Exception $e) {
                return json(['code' => 1,'msg' => $e->getMessage()]);
            }
        }
    }
}
